package labox.innovation.gameserver.model;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javolution.util.FastList;
import javolution.util.FastMap;
import labox.innovation.config.Config;
import labox.innovation.gameserver.model.actor.L2Character;
import labox.innovation.gameserver.model.actor.L2Playable;
import labox.innovation.gameserver.model.actor.L2Summon;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId; //import labox.innovation.gameserver.network.serverpackets.AbnormalStatusUpdate;
//import labox.innovation.gameserver.network.serverpackets.PartySpelled;
import labox.innovation.gameserver.network.serverpackets.AbnormalStatusUpdate;
import labox.innovation.gameserver.network.serverpackets.PartySpelled;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.templates.skills.L2EffectType;

//效果链
//管理所有的效果 如：效果的叠加， 效果的顺序， 效果更新
public class CharEffectList {

	private static final L2Effect[] EMPTY_EFFECTS = new L2Effect[0];

	/***
	 * 以下 2个数据结构 保存所有的效果 效果类型分为2大类， buff(有益的) 和 debuff(有害的)型
	 */
	// buff类的效果
	private FastList<L2Effect> _buffs;
	// debuff类效果
	private FastList<L2Effect> _debuffs;

	// The table containing the List of all stacked effect in progress for each Stack group Identifier
	// 保存正在处理的效果链
	// 根据效果的叠加类型分类
	// 叠加类型 来自于 effect中的 StackType字段
	private Map<String, List<L2Effect>> _stackedEffects;

	// 效果队列是否已初始化
	private boolean _queuesInitialized;

	// 增加效果队列
	private LinkedBlockingQueue<L2Effect> _addQueue;
	// 移除效果队列
	private LinkedBlockingQueue<L2Effect> _removeQueue;
	// 修改效果队列的原子锁
	private AtomicBoolean queueLock = new AtomicBoolean();

	// only party icons need to be updated
	// 是否 只有团队buff图标需要更新
	private boolean _partyOnly = false;

	// Owner of this list
	// 效果链的 拥有者
	private L2Character _owner;

	public CharEffectList(L2Character owner) {
		_owner = owner;
	}

	/**
	 * Returns all effects affecting stored in this CharEffectList
	 * 
	 * @return
	 */
	// 返回所有的效果
	public final L2Effect[] getAllEffects() {
		// If no effect is active, return EMPTY_EFFECTS
		if ((_buffs == null || _buffs.isEmpty()) && (_debuffs == null || _debuffs.isEmpty())) {
			return EMPTY_EFFECTS;
		}

		// Create a copy of the effects
		FastList<L2Effect> temp = new FastList<L2Effect>();

		// Add all buffs and all debuffs
		if (_buffs != null) {
			synchronized (_buffs) {
				if (!_buffs.isEmpty())
					temp.addAll(_buffs);
			}
		}
		if (_debuffs != null) {
			synchronized (_debuffs) {
				if (!_debuffs.isEmpty())
					temp.addAll(_debuffs);
			}
		}

		// Return all effects in an array
		L2Effect[] tempArray = new L2Effect[temp.size()];
		temp.toArray(tempArray);
		return tempArray;
	}

	/**
	 * Returns the first effect matching the given EffectType
	 * 
	 * @param tp
	 * @return
	 */
	// 根据效果类型， 返回第一个匹配此类型的已经使用了的效果
	public final L2Effect getFirstEffect(L2EffectType tp) {
		L2Effect effectNotInUse = null;

		if (_buffs != null) {
			synchronized (_buffs) {
				if (!_buffs.isEmpty()) {
					for (L2Effect e : _buffs) {
						if (e == null)
							continue;
						if (e.getEffectType() == tp) {
							if (e.getInUse())
								return e;
							else
								effectNotInUse = e;
						}
					}
				}
			}
		}
		if (effectNotInUse == null && _debuffs != null) {
			synchronized (_debuffs) {
				if (!_debuffs.isEmpty()) {
					for (L2Effect e : _debuffs) {
						if (e == null)
							continue;
						if (e.getEffectType() == tp) {
							if (e.getInUse())
								return e;
							else
								effectNotInUse = e;
						}
					}
				}
			}
		}
		return effectNotInUse;
	}

	/**
	 * Returns the first effect matching the given L2Skill
	 * 
	 * @param skill
	 * @return
	 */
	// 根据技能类型，返回第一个匹配此技能，且已使用的效果
	public final L2Effect getFirstEffect(L2Skill skill) {
		L2Effect effectNotInUse = null;

		if (skill.isDebuff()) {
			if (_debuffs == null)
				return null;

			synchronized (_debuffs) {
				if (_debuffs.isEmpty())
					return null;

				for (L2Effect e : _debuffs) {
					if (e == null)
						continue;
					if (e.getSkill() == skill) {
						if (e.getInUse())
							return e;
						else
							effectNotInUse = e;
					}
				}
			}
			return effectNotInUse;
		} else {
			if (_buffs == null)
				return null;

			synchronized (_buffs) {
				if (_buffs.isEmpty())
					return null;

				for (L2Effect e : _buffs) {
					if (e == null)
						continue;
					if (e.getSkill() == skill) {
						if (e.getInUse())
							return e;
						else
							effectNotInUse = e;
					}
				}
			}
			return effectNotInUse;
		}
	}

	/**
	 * Returns the first effect matching the given skillId
	 * 
	 * @param index
	 * @return
	 */
	// 根据技能ID，返回第一个匹配此技能ID，且已使用的效果
	public final L2Effect getFirstEffect(int skillId) {
		L2Effect effectNotInUse = null;

		if (_buffs != null) {
			synchronized (_buffs) {
				if (!_buffs.isEmpty()) {
					for (L2Effect e : _buffs) {
						if (e == null)
							continue;
						if (e.getSkill().getId() == skillId) {
							if (e.getInUse())
								return e;
							else
								effectNotInUse = e;
						}
					}
				}
			}
		}

		if (effectNotInUse == null && _debuffs != null) {
			synchronized (_debuffs) {
				if (!_debuffs.isEmpty()) {
					for (L2Effect e : _debuffs) {
						if (e == null)
							continue;
						if (e.getSkill().getId() == skillId) {
							if (e.getInUse())
								return e;
							else
								effectNotInUse = e;
						}
					}
				}
			}
		}

		return effectNotInUse;
	}

	/**
	 * Checks if the given skill stacks with an existing one.
	 * 
	 * @param checkSkill the skill to be checked
	 * 
	 * @return Returns whether or not this skill will stack
	 */
	// 检查 效果栈中是否已包含此类型
	// 貌似只检查 有益状态buff
	private boolean doesStack(L2Skill checkSkill) {
		if ((_buffs == null || _buffs.isEmpty()) || checkSkill._effectTemplates == null || checkSkill._effectTemplates.length < 1 || checkSkill._effectTemplates[0].stackType == null || "none".equals(checkSkill._effectTemplates[0].stackType)) {
			return false;
		}

		// ?
		String stackType = checkSkill._effectTemplates[0].stackType;

		for (L2Effect e : _buffs) {
			if (e.getStackType() != null && e.getStackType().equals(stackType)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Return the number of buffs in this CharEffectList not counting Songs/Dances
	 * 
	 * @return
	 */
	// 返回状态buff类型效果的数量
	//
	public int getBuffCount() {
		if (_buffs == null)
			return 0;
		int buffCount = 0;

		synchronized (_buffs) {
			if (_buffs.isEmpty())
				return 0;

			for (L2Effect e : _buffs) {
				// 状态类型都会显示图标.
				// Songs/Dances 不在统计之列
				if (e != null && e.getShowIcon()) {
					switch (e.getSkill().getSkillType()) {
					case BUFF:
					case REFLECT:
					case HEAL_PERCENT:
					case MANAHEAL_PERCENT:
						buffCount++;
					}
				}
			}
		}
		return buffCount;
	}

	/**
	 * Return the number of Songs/Dances in this CharEffectList
	 * 
	 * @return
	 */
	// 返回Songs/Dances的buff类型数量
	public int getDanceCount() {
		if (_buffs == null)
			return 0;
		int danceCount = 0;

		synchronized (_buffs) {
			if (_buffs.isEmpty())
				return 0;

			for (L2Effect e : _buffs) {
				if (e != null && e.getInUse())
					danceCount++;
			}
		}
		return danceCount;
	}

	/**
	 * Exits all effects in this CharEffectList
	 */
	// 结束所有的效果
	public final void stopAllEffects() {
		// Get all active skills effects from this list
		L2Effect[] effects = getAllEffects();

		// Exit them
		for (L2Effect e : effects) {
			if (e != null)
				e.exit(true);
		}
	}

	/**
	 * Exits all effects in this CharEffectList
	 */
	// 结束所有效果，除了死亡之后还会存在的效果
	public final void stopAllEffectsExceptThoseThatLastThroughDeath() {
		// Get all active skills effects from this list
		L2Effect[] effects = getAllEffects();

		// Exit them
		for (L2Effect e : effects) {
			if (e != null && !e.getSkill().isStayAfterDeath())
				e.exit(true);
		}
	}

	/**
	 * Exit all effects having a specified type
	 * 
	 * @param type
	 */
	// 结束指定效果类型的 所有效果
	public final void stopEffects(L2EffectType type) {
		// Go through all active skills effects
		FastList<L2Effect> temp = new FastList<L2Effect>();
		if (_buffs != null) {
			synchronized (_buffs) {
				if (!_buffs.isEmpty()) {
					for (L2Effect e : _buffs)
						// Get active skills effects of the selected type
						if (e != null && e.getEffectType() == type)
							temp.add(e);
				}
			}
		}
		if (_debuffs != null) {
			synchronized (_debuffs) {
				if (!_debuffs.isEmpty()) {
					for (L2Effect e : _debuffs)
						// Get active skills effects of the selected type
						if (e != null && e.getEffectType() == type)
							temp.add(e);
				}
			}
		}
		if (temp != null && !temp.isEmpty()) {
			for (L2Effect e : temp)
				if (e != null)
					e.exit();
		}
	}

	/**
	 * 结束负面效果， 根据 效果类型
	 * 
	 * @param type
	 */
	public final boolean stopBuffEffect(int skillId, L2EffectType type) {
		if (type == null) {
			return false;
		}
		L2Effect temp = null;

		if (_buffs != null) {
			synchronized (_buffs) {
				if (!_buffs.isEmpty()) {
					for (L2Effect e : _buffs)
						// Get active skills effects of the selected type
						if (e != null && e.getEffectType() == type && e.getSkill().getId() == skillId) {
							temp = e;
							break;
						}

				}
			}
		}

		if (temp != null) {
			temp.exit();
			return true;
		}

		return false;
	}

	/**
	 * Exits all effects created by a specific skillId
	 * 
	 * @param skillId
	 */
	// 结束 因此技能而触发的所有效果
	public final void stopSkillEffects(int skillId) {
		// Go through all active skills effects
		FastList<L2Effect> temp = new FastList<L2Effect>();
		if (_buffs != null) {
			synchronized (_buffs) {
				if (!_buffs.isEmpty()) {
					for (L2Effect e : _buffs)
						if (e != null && e.getSkill().getId() == skillId)
							temp.add(e);
				}
			}
		}
		if (_debuffs != null) {
			synchronized (_debuffs) {
				if (!_debuffs.isEmpty()) {
					for (L2Effect e : _debuffs)
						if (e != null && e.getSkill().getId() == skillId)
							temp.add(e);
				}
			}
		}
		if (temp != null && !temp.isEmpty()) {
			for (L2Effect e : temp)
				if (e != null)
					e.exit();
		}
	}

	/**
	 * 是否仅 更新团队图标
	 * 
	 * @param partyOnly
	 */
	public void updateEffectIcons(boolean partyOnly) {
		if (_buffs == null && _debuffs == null)
			return;

		if (partyOnly)
			_partyOnly = true;

		queueRunner();
	}

	/***
	 * 把效果入队列
	 * 
	 * @param effect
	 * @param remove true : 加入效果 ； false : 移除效果
	 */
	public void queueEffect(L2Effect effect, boolean remove) {
		if (effect == null)
			return;

		if (!_queuesInitialized)
			init();

		// 是移除命令 那么加入移除效果队列
		// 否则加入到 加入效果队列
		if (remove)
			_removeQueue.offer(effect);
		else
			_addQueue.offer(effect);

		// 执行效果
		queueRunner();

	}// end queueEffect

	synchronized private void init() {
		_addQueue = new LinkedBlockingQueue<L2Effect>();
		_removeQueue = new LinkedBlockingQueue<L2Effect>();
		_queuesInitialized = true;
	}

	/***
	 * 执行效果队列 总是先执行 移除队列中的效果， 再执行 加入队列中的效果
	 * 
	 */
	private void queueRunner() {
		if (!queueLock.compareAndSet(false, true))
			return;

		try {
			L2Effect effect;
			do {
				// remove has more priority than add
				// so removing all effects from queue first
				while ((effect = _removeQueue.poll()) != null) {
					removeEffectFromQueue(effect);
					_partyOnly = false;
				}

				if ((effect = _addQueue.poll()) != null) {
					addEffectFromQueue(effect);
					_partyOnly = false;
				}
			} while (!_addQueue.isEmpty() || !_removeQueue.isEmpty());

			// TODO:
			// 不在每次 发送更新buff图标消息
			// 只有在 客户端请求时才发送
			// updateEffectIcons();
		} finally {
			queueLock.set(false);
		}

	}// end queueRunner

	/***
	 * 移除效果
	 * 
	 * @param effect
	 */
	protected void removeEffectFromQueue(L2Effect effect) {
		if (effect == null)
			return;

		FastList<L2Effect> effectList;

		// 确定效果的种类
		if (effect.getSkill().isDebuff()) {
			if (_debuffs == null)
				return;
			effectList = _debuffs;
		} else {
			if (_buffs == null)
				return;
			effectList = _buffs;
		}

		// System.out.println("移除效果:" + effect);

		// 如果效果叠加类型为空， 那么移除掉此效果类型的 状态计算函数
		if ("none".equals(effect.getStackType())) {
			// Remove Func added by this effect from the L2Character Calculator
			_owner.removeStatsOwner(effect);
		} else {
			if (_stackedEffects == null)
				return;

			// Get the list of all stacked effects corresponding to the stack type of the L2Effect to add
			List<L2Effect> stackQueue = _stackedEffects.get(effect.getStackType());

			if (stackQueue == null || stackQueue.isEmpty())
				return;

			int index = stackQueue.indexOf(effect);

			// Remove the effect from the stack group
			if (index >= 0) {
				stackQueue.remove(effect);

				// Check if the first stacked effect was the effect to remove
				// ?????
				if (index == 0) {
					// 如果是第一个效果， 那么需要一在角色身上移除掉 这些效果的计算公式
					// 第一个效果 ，永远是当前正在起作用的效果
					// Remove all its Func objects from the L2Character calculator set
					_owner.removeStatsOwner(effect);

					// Check if there's another effect in the Stack Group
					if (!stackQueue.isEmpty()) {
						L2Effect newStackedEffect = listsContains(stackQueue.get(0));
						if (newStackedEffect != null) {
							// Set the effect to In Use
							if (newStackedEffect.setInUse(true))
								// Add its list of Funcs to the Calculator set of the L2Character
								_owner.addStatFuncs(newStackedEffect.getStatFuncs());
						}
					}
				}// end if (index >= 0)

				// 如果此叠加类型 效果队列为空的话 ，那么直接移除掉
				// 否则更新效果队列
				if (stackQueue.isEmpty())
					_stackedEffects.remove(effect.getStackType());
				else
					// Update the Stack Group table _stackedEffects of the L2Character
					_stackedEffects.put(effect.getStackType(), stackQueue);
			}

		}// if ("none".equals(effect.getStackType()))

		// Remove the active skill L2effect from _effects of the L2Character
		// 从buff 或 debuff中移除掉此效果
		if (effectList.remove(effect) && _owner instanceof FuzePcInstance && effect.getShowIcon()) {
			SystemMessage sm;
			sm = new SystemMessage(SystemMessageId.EFFECT_S1_DISAPPEARED);
			sm.addSkillName(effect);
			_owner.sendPacket(sm);
		}
	}

	/***
	 * 添加效果
	 * 
	 * @param newEffect
	 */
	protected void addEffectFromQueue(L2Effect newEffect) {
		if (newEffect == null)
			return;

		L2Skill newSkill = newEffect.getSkill();

		//
		if (newSkill.isDebuff()) {
			if (_debuffs == null)
				_debuffs = new FastList<L2Effect>();
			for (L2Effect e : _debuffs) {
				// 技能id相同，也就是同一技能施放的效果
				// 效果类型相同 ，StackOrder相同等
				if (e != null && e.getSkill().getId() == newEffect.getSkill().getId() && e.getEffectType() == newEffect.getEffectType() && e.getStackOrder() == newEffect.getStackOrder() && e.getStackType().equals(newEffect.getStackType())) {
					// Started scheduled timer needs to be canceled.
					newEffect.stopEffectTask();
					return;
				}
			}
			_debuffs.addLast(newEffect);
		} else {
			if (_buffs == null)
				_buffs = new FastList<L2Effect>();

			// 效果是有益buff，且同样的效果已存在， 那么覆盖掉以前的效果(更新效果)
			//
			for (L2Effect e : _buffs) {
				if (e != null && e.getSkill().getId() == newEffect.getSkill().getId() && e.getEffectType() == newEffect.getEffectType() && e.getStackOrder() == newEffect.getStackOrder() && e.getStackType().equals(newEffect.getStackType())) {
					e.exit(); // exit this
				}
			}//

			// if max buffs, no herb effects are used, even if they would replace one old
			// 如果有益状态数量(buff)已经达到最大值， 且newEffect是一个HerbEffect， 那么结束此效果
			//
			if (newEffect.isHerbEffect() && getBuffCount() >= _owner.getMaxBuffCount()) {
				newEffect.stopEffectTask();
				return;
			}

			// Remove first buff when buff list is full
			if (!doesStack(newSkill)) {
				int effectsToRemove;

				// 如果状态buff达到最大数量， 前面的效果全部结束
				effectsToRemove = getBuffCount() - _owner.getMaxBuffCount();
				if (effectsToRemove >= 0) {
					switch (newSkill.getSkillType()) {
					case BUFF:
					case REFLECT:
					case HEAL_PERCENT:
					case MANAHEAL_PERCENT:
						for (L2Effect e : _buffs) {
							if (e == null)
								continue;

							switch (e.getSkill().getSkillType()) {
							case BUFF:
							case REFLECT:
							case HEAL_PERCENT:
							case MANAHEAL_PERCENT:
								e.exit();
								effectsToRemove--;
								break; // break switch()
							default:
								continue; // continue for()
							}
							if (effectsToRemove < 0)
								break; // break for()
						}
					}

				}// if (effectsToRemove >= 0)

			}// if (!doesStack(newSkill) && !newSkill.is7Signs())

			// Icons order: buffs, 7s, toggles, dances
			// 把新效果加入
			// buff的顺序 : 普通， 7s， toggles, dances
			// TODO: 效果叠加需要修改
			// int pos = 0;
			// // normal buff - before toggles and 7s and dances
			// for (L2Effect e : _buffs) {
			// if (e == null)
			// continue;
			// if (e.getSkill().isToggle() || e.getSkill().is7Signs() || e.getSkill().isDance())
			// break;
			// pos++;
			// }
			//			
			// _buffs.add(pos, newEffect);
			//
			_buffs.addLast(newEffect);

		}// end if (newSkill.isDebuff()) else

		// Check if a stack group is defined for this effect
		// 如果没有为此效果指定叠加类型， 那么直接使用这个效果 不加入到stackGroup中去
		if ("none".equals(newEffect.getStackType())) {
			// Set this L2Effect to In Use
			if (newEffect.setInUse(true))
				// Add Funcs of this effect to the Calculator set of the L2Character
				_owner.addStatFuncs(newEffect.getStatFuncs());

			return;
		}

		// 指定了效果叠加类型
		List<L2Effect> stackQueue;
		L2Effect effectToAdd = null;
		L2Effect effectToRemove = null;
		if (_stackedEffects == null)
			_stackedEffects = new FastMap<String, List<L2Effect>>();

		// Get the list of all stacked effects corresponding to the stack type of the L2Effect to add
		stackQueue = _stackedEffects.get(newEffect.getStackType());

		if (stackQueue != null) {
			int pos = 0;
			if (!stackQueue.isEmpty()) {
				// Get the first stacked effect of the Stack group selected
				effectToRemove = listsContains(stackQueue.get(0));

				// Create an Iterator to go through the list of stacked effects in progress on the L2Character
				Iterator<L2Effect> queueIterator = stackQueue.iterator();

				// 根据效果的 order 插入到效果链中
				while (queueIterator.hasNext()) {
					if (newEffect.getStackOrder() < queueIterator.next().getStackOrder())
						pos++;
					else
						break;
				}
				// Add the new effect to the Stack list in function of its position in the Stack group
				stackQueue.add(pos, newEffect);

				// skill.exit() could be used, if the users don't wish to see "effect
				// removed" always when a timer goes off, even if the buff isn't active
				// any more (has been replaced). but then check e.g. npc hold and raid petrification.
				// 为什么要移除掉?
				if (Config.EFFECT_CANCELING && !newEffect.isHerbEffect() && stackQueue.size() > 1) {
					if (newSkill.isDebuff()) {
						_debuffs.remove(stackQueue.remove(1));
					} else {
						_buffs.remove(stackQueue.remove(1));
					}
				}// end if (Config.EFFECT_CANCELING && !newEffect.isHerbEffect() && stackQueue.size() > 1)

			}// end if (!stackQueue.isEmpty())
			else {
				stackQueue.add(0, newEffect);
			}// end if (!stackQueue.isEmpty()) else

		} else {
			stackQueue = new FastList<L2Effect>();
			stackQueue.add(0, newEffect);
		}// end if (stackQueue != null) else

		// Update the Stack Group table _stackedEffects of the L2Character
		// 更新叠加类型Stack Gruop的值
		_stackedEffects.put(newEffect.getStackType(), stackQueue);

		// Get the first stacked effect of the Stack group selected
		if (stackQueue != null && !stackQueue.isEmpty()) {
			effectToAdd = listsContains(stackQueue.get(0));
		}

		if (effectToRemove != effectToAdd) {
			if (effectToRemove != null) {
				// Remove all Func objects corresponding to this stacked effect from the Calculator set of the L2Character
				_owner.removeStatsOwner(effectToRemove);

				// Set the L2Effect to Not In Use
				effectToRemove.setInUse(false);
			}
			if (effectToAdd != null) {
				// Set this L2Effect to In Use
				if (effectToAdd.setInUse(true))
					// Add all Func objects corresponding to this stacked effect to the Calculator set of the L2Character
					_owner.addStatFuncs(effectToAdd.getStatFuncs());
			}
		}
	}// end

	/**
	 * 
	 * @return
	 */
	public FastList<L2Effect> getBuffs() {
		return _buffs;
	}

	public FastList<L2Effect> getDebuffs() {
		return _debuffs;
	}

	/**
	 * 更新效果图标
	 */
	protected void updateEffectIcons() {
		if (_owner == null || !(_owner instanceof L2Playable))
			return;

		AbnormalStatusUpdate mi = null;
		PartySpelled ps = null;

		if (_owner instanceof FuzePcInstance) {
			if (_partyOnly)
				_partyOnly = false;
			else
				mi = new AbnormalStatusUpdate();

			if (_owner.isInParty())
				ps = new PartySpelled(_owner);
		} else if (_owner instanceof L2Summon)
			ps = new PartySpelled(_owner);

		if (_buffs != null && !_buffs.isEmpty()) {
			synchronized (_buffs) {
				for (L2Effect e : _buffs) {
					if (e == null || !e.getShowIcon())
						continue;

					switch (e.getEffectType()) {
					case SIGNET_GROUND:
						continue;
					}

					if (e.getInUse()) {
						if (mi != null)
							e.addIcon(mi);

						if (ps != null)
							e.addPartySpelledIcon(ps);
					}
				}
			}
		}
		if (_debuffs != null && !_debuffs.isEmpty()) {
			synchronized (_debuffs) {
				for (L2Effect e : _debuffs) {
					if (e == null || !e.getShowIcon())
						continue;

					switch (e.getEffectType()) {
					case SIGNET_GROUND:
						continue;
					}

					if (e.getInUse()) {
						if (mi != null)
							e.addIcon(mi);

						if (ps != null)
							e.addPartySpelledIcon(ps);
					}
				}
			}
		}

		if (mi != null)
			_owner.sendPacket(mi);

		if (ps != null) {
			if (_owner instanceof L2Summon) {
				FuzePcInstance summonOwner = ((L2Summon) _owner).getOwner();

				if (summonOwner != null) {
					if (summonOwner.isInParty())
						summonOwner.getParty().broadcastToPartyMembers(ps);
					else
						summonOwner.sendPacket(ps);
				}
			} else if (_owner instanceof FuzePcInstance && _owner.isInParty())
				_owner.getParty().broadcastToPartyMembers(ps);
		}
	}

	/**
	 * Returns effect if contains in _buffs or _debuffs and null if not found
	 * 
	 * @param effect
	 * @return
	 */
	private L2Effect listsContains(L2Effect effect) {
		if (_buffs != null && !_buffs.isEmpty() && _buffs.contains(effect))
			return effect;
		if (_debuffs != null && !_debuffs.isEmpty() && _debuffs.contains(effect))
			return effect;
		return null;
	}

	/**
	 * Clear and null all queues and lists Use only during delete character from the world.
	 */
	public void clear() {
		try {
			if (_addQueue != null) {
				_addQueue.clear();
				_addQueue = null;
			}
			if (_removeQueue != null) {
				_removeQueue.clear();
				_removeQueue = null;
			}
			_queuesInitialized = false;

			if (_buffs != null) {
				_buffs.clear();
				_buffs = null;
			}
			if (_debuffs != null) {
				_debuffs.clear();
				_debuffs = null;
			}

			if (_stackedEffects != null) {
				_stackedEffects.clear();
				_stackedEffects = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
